﻿using System;
using System.Linq;
using System.IO;
using System.Windows;
using System.Windows.Controls;

using Perseus;
using Perseus.Data;
using Perseus.Plugins;

using Hetymine.Executioner.Data;
using Hetymine.Executioner.Plugins;

/**
 * TODO:
 * - Add ability to define custom command for display so that users can use existing command or
 * choose to open text data in editor of their choice..
 */
namespace Hetymine.Executioner.Controls {
    /// <summary>
    /// Interaction logic for BehaviourConfigurator.xaml
    /// </summary>
    public partial class ExecutionerConfigurator : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;
        private bool _IsApply = true;
        private Microsoft.Win32.OpenFileDialog _SelectSound;

        public ExecutionerConfigurator() {
            InitializeComponent();

            this.cSaveHistory.Click += new RoutedEventHandler(SaveHistory_Click);
            this.cMaxHistoryItems.TextChanged += new TextChangedEventHandler(MaxHistoryItems_TextChanged);

            this.cDefaultDisplay.SelectionChanged += new SelectionChangedEventHandler(DefaultDisplay_SelectionChanged);
                       
            this.cError.TextChanged += new TextChangedEventHandler(Sound_TextChanged);
            this.cErrorNotify.TextChanged += new TextChangedEventHandler(Sound_TextChanged);
            this.cSuccess.TextChanged += new TextChangedEventHandler(Sound_TextChanged);
            this.cSuccessNotify.TextChanged += new TextChangedEventHandler(Sound_TextChanged);

            this.cSelectError.Click += new RoutedEventHandler(SelectSound_Click);
            this.cSelectErrorNotify.Click += new RoutedEventHandler(SelectSound_Click);
            this.cSelectSuccess.Click += new RoutedEventHandler(SelectSound_Click);
            this.cSelectSuccessNotify.Click += new RoutedEventHandler(SelectSound_Click);

            this.cResetError.Click += new RoutedEventHandler(ResetSound_Click);
            this.cResetErrorNotify.Click += new RoutedEventHandler(ResetSound_Click);
            this.cResetSuccess.Click += new RoutedEventHandler(ResetSound_Click);
            this.cResetSuccessNotify.Click += new RoutedEventHandler(ResetSound_Click);

            this.cPlayError.Click += new RoutedEventHandler(PlaySound_Click);
            this.cPlayErrorNotify.Click += new RoutedEventHandler(PlaySound_Click);
            this.cPlaySuccess.Click += new RoutedEventHandler(PlaySound_Click);
            this.cPlaySuccessNotify.Click += new RoutedEventHandler(PlaySound_Click);

            this._SelectSound = new Microsoft.Win32.OpenFileDialog();
            this._SelectSound.Filter = "All Files (*.*)|*.*|Sound Files (*.wav)|*.wav";
        }

        private void SaveHistory_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetBool("Executioner", "Save History", cSaveHistory.IsChecked.Value);
        }
        private void MaxHistoryItems_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cMaxHistoryItems.Text, out value)) {
                value = 250;
            }
            value = Math.Max(-1, value);

            this._Config.SetInt("Executioner", "Max History Items", value);
        }

        private void DefaultDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetString("Executioner", "Display Plugin", this.cDefaultDisplay.SelectedPlugin);
        }

        private void Sound_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            TextBox tb = sender as TextBox;

            switch (tb.Name) {
                case "cError":
                    this._Config.SetString("Executioner.Sounds", "Error", tb.Text);
                    break;
                case "cErrorNotify":
                    this._Config.SetString("Executioner.Sounds", "Error Notify", tb.Text);
                    break;
                case "cSuccess":
                    this._Config.SetString("Executioner.Sounds", "Success", tb.Text);
                    break;
                case "cSuccessNotify":
                    this._Config.SetString("Executioner.Sounds", "Success Notify", tb.Text);
                    break;
            }
        }
        private void SelectSound_Click(object sender, RoutedEventArgs e) {
            Button b = sender as Button;
            TextBox tb = null;

            switch (b.Name) {
                case "cSelectError":
                    tb = this.cError;
                    break;
                case "cSelectErrorNotify":
                    tb = this.cErrorNotify;
                    break;
                case "cSelectSuccess":
                    tb = this.cSuccess;
                    break;
                case "cSelectSuccessNotify":
                    tb = this.cSuccessNotify;
                    break;
            }

            if (File.Exists(tb.Text)) {
                this._SelectSound.FileName = tb.Text;
            }
            else {
                this._SelectSound.FileName = string.Empty;
            }
            if (this._SelectSound.ShowDialog().Value) {
                tb.Text = this._SelectSound.FileName;
            }
        }
        private void ResetSound_Click(object sender, RoutedEventArgs e) {
            Button b = sender as Button;

            switch (b.Name) {
                case "cResetError":
                    this.cError.Text = string.Empty;
                    break;
                case "cResetErrorNotify":
                    this.cErrorNotify.Text = string.Empty;
                    break;
                case "cResetSuccess":
                    this.cSuccess.Text = string.Empty;
                    break;
                case "cResetSuccessNotify":
                    this.cSuccessNotify.Text = string.Empty;
                    break;
            }
        }
        private void PlaySound_Click(object sender, RoutedEventArgs e) {
            Button b = sender as Button;

            switch (b.Name) {
                case "cPlayError":
                    Executioner.Sounds.Error(this.cError.Text);
                    break;
                case "cPlayErrorNotify":
                    Executioner.Sounds.ErrorNotify(this.cErrorNotify.Text);
                    break;
                case "cPlaySuccess":
                    Executioner.Sounds.Success(this.cSuccess.Text);
                    break;
                case "cPlaySuccessNotify":
                    Executioner.Sounds.SuccessNotify(this.cSuccessNotify.Text);
                    break;
            }
        }

        #region IConfigurator<IniFile> Members
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this._IsApply = false;

            // Command Histroy
            this.cSaveHistory.IsChecked = this._Config.GetBool("Executioner", "Save History", true);
            this.cMaxHistoryItems.Text = this._Config.GetInt("Executioner", "Max History Items", 250).ToString();

            // Default Display Plugin
            this.cDefaultDisplay.SelectedPlugin = this._Config.GetString("Executioner", "Display Plugin", string.Empty);

            // Sounds
            this.cError.Text = this._Config.GetString("Executioner.Sounds", "Error", string.Empty);
            this.cErrorNotify.Text = this._Config.GetString("Executioner.Sounds", "Error Notify", string.Empty);
            this.cSuccess.Text = this._Config.GetString("Executioner.Sounds", "Success", string.Empty);
            this.cSuccessNotify.Text = this._Config.GetString("Executioner.Sounds", "Success Notify", string.Empty);

            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
